package com.lishu.carsafe.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lishu.carsafe.R;
import com.lishu.carsafe.activity.ScheduleActivity;
import com.lishu.carsafe.model.ScheduleListModel;
import com.lishu.carsafe.model.ScheduleListModel.ScheduleBean;
import com.lishu.carsafe.model.entity.Schedule;
import com.lishu.carsafe.model.entity.Shift;

import java.util.List;

/**
 * describe:
 *
 * @author:ZhangWei(张伟) date:2020/5/18
 * time:15:06
 * motto:高山仰止，景行行止，虽不能至，心向往之。
 */
public class ScheduleAdapter extends RecyclerView.Adapter<ScheduleAdapter.ScheduleViewHolder> {
    private static final int TYPE_POSITIVE_COLOR = 0;//正常颜色
    private static final int TYPE_GREY_COLOR = 1;//置灰

    private ScheduleActivity context;
    private List<Schedule> data;
    private LayoutInflater inflater;
    String dateTime = "";

    public ScheduleAdapter(Context context) {
        this.context = (ScheduleActivity) context;
        inflater = LayoutInflater.from(context);
    }

    public void setData(List<Schedule> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public void setDate(String dateTime) {
        this.dateTime = dateTime;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ScheduleViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = inflater.inflate(R.layout.activity_schedule_item, viewGroup, false);
        ScheduleViewHolder viewHolder = new ScheduleViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ScheduleViewHolder scheduleViewHolder, int i) {
        if (data == null || data.size() == 0) {
            //无排班的布局
            scheduleViewHolder.ll_item_empty.setVisibility(View.VISIBLE);
            scheduleViewHolder.ll_item_content.setVisibility(View.GONE);
            scheduleViewHolder.tv_item_week.setText(dateTime);
        } else {
            //有排班的布局
            scheduleViewHolder.ll_item_empty.setVisibility(View.GONE);
            scheduleViewHolder.ll_item_content.setVisibility(View.VISIBLE);
            scheduleViewHolder.tv_item_week.setText(dateTime);
            final Schedule schedule = data.get(i);
            if (i > 0) {
                scheduleViewHolder.tv_item_week.setVisibility(View.GONE);
            } else {
                scheduleViewHolder.tv_item_week.setVisibility(View.VISIBLE);
            }
            scheduleViewHolder.tv_item_start.setText(schedule.getStartTime());
            scheduleViewHolder.tv_item_end.setText(schedule.getEndTime());
            scheduleViewHolder.tv_item_name.setText(schedule.getVehicle().getName());
            scheduleViewHolder.tv_item_line.setText(schedule.getLine().getName());
            scheduleViewHolder.tv_item_map.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //跳转到地图界面 传递数据过去
                    context.bdMapActivity(schedule.getLine().getId(),schedule.getLine().getName());
                }
            });

            //0定点发车1循环驾驶
            switch (schedule.getType()) {
                case 0:
                    scheduleViewHolder.tv_item_cycle_driving.setText("定点发车");
                    scheduleViewHolder.view_item.setBackgroundColor(0xFF00A0E9);
                    scheduleViewHolder.tv_item_cycle_driving.setTextColor(0xFF00A0E9);
                    scheduleViewHolder.ll_schedule_start.setVisibility(View.VISIBLE);
                    //动态增加布局
                    addView(scheduleViewHolder,schedule.getShifts(),schedule);
                    //首班车
//                    scheduleViewHolder.tv_item_point_start.setText(schedule.getShifts().get(0).getShiftTime());
//                    scheduleViewHolder.tv_item_point_first.setText(schedule.getShifts().get(0).getRemark());
                    //末班车
//                    scheduleViewHolder.tv_item_point_end.setText(schedule.getShifts().get(1).getShiftTime());
//                    scheduleViewHolder.tv_item_point_last.setText(schedule.getShifts().get(1).getRemark());
                    break;
                case 1:
                    scheduleViewHolder.tv_item_cycle_driving.setText("循环驾驶");
                    scheduleViewHolder.view_item.setBackgroundColor(0xFF00ADB5);
                    scheduleViewHolder.tv_item_cycle_driving.setTextColor(0xFF00ADB5);
                    scheduleViewHolder.ll_schedule_start.setVisibility(View.GONE);
                    break;
            }
            //已接受的布局
            scheduleViewHolder.ll_item_accept.setVisibility(View.GONE);
            //已完成的布局
            scheduleViewHolder.ll_item_complete.setVisibility(View.GONE);
            //正常颜色
            changeColor(scheduleViewHolder, TYPE_POSITIVE_COLOR);
            //state 1已接受2请假3转班4已完成
            switch (schedule.getScheduleOfficer().getState()) {
                case 0:
                case 1:
                    scheduleViewHolder.ll_item_content.setAlpha(1.0f);
                    //显示已接受
                    scheduleViewHolder.ll_item_accept.setVisibility(View.VISIBLE);
                    //隐藏已完成
                    scheduleViewHolder.ll_item_complete.setVisibility(View.GONE);
                    scheduleViewHolder.tv_item_accept.setText("已接受");
                    //请假
                    scheduleViewHolder.tv_item_leave.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            //弹出弹出
                            context.showLeave(schedule.getScheduleOfficer().getId());
                        }
                    });
                    //签到
                    scheduleViewHolder.tv_item_sign_in.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            context.signInActivity(false, schedule.getScheduleOfficer().getId());
                        }
                    });
                    break;
                case 2: //请假的布局全部颜色改成灰色 #CCCCCC
                    scheduleViewHolder.ll_item_accept.setVisibility(View.GONE);
                    scheduleViewHolder.ll_item_complete.setVisibility(View.VISIBLE);
                    scheduleViewHolder.tv_item_complete.setText("已请假");
                    scheduleViewHolder.tv_item_check_in.setVisibility(View.VISIBLE);
                    scheduleViewHolder.tv_item_check_in.setText("取消请假");
                    scheduleViewHolder.ll_item_content.setAlpha(0.6f);
                    //置灰
                    changeColor(scheduleViewHolder, TYPE_GREY_COLOR);
                    scheduleViewHolder.tv_item_check_in.setAlpha(1.0f);
                    scheduleViewHolder.tv_item_check_in.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            //取消签到并刷新界面
                            context.cancelLeave(schedule.getScheduleOfficer().getId());
                        }
                    });
                    break;
                case 3://请假成功
                    scheduleViewHolder.ll_item_accept.setVisibility(View.GONE);
                    scheduleViewHolder.ll_item_complete.setVisibility(View.VISIBLE);
                    scheduleViewHolder.tv_item_complete.setText("已请假");
                    scheduleViewHolder.tv_item_check_in.setVisibility(View.GONE);
                    scheduleViewHolder.ll_item_content.setAlpha(0.6f);
                    //置灰
                    changeColor(scheduleViewHolder, TYPE_GREY_COLOR);
                    scheduleViewHolder.tv_item_check_in.setAlpha(1.0f);
                    break;
                case 4:
                    scheduleViewHolder.ll_item_content.setAlpha(1.0f);
                    scheduleViewHolder.tv_item_complete.setTextColor(0xFF00ADB5);
                    //隐藏已接受
                    scheduleViewHolder.ll_item_accept.setVisibility(View.GONE);
                    //显示已完成
                    scheduleViewHolder.ll_item_complete.setVisibility(View.VISIBLE);
                    scheduleViewHolder.tv_item_complete.setText("已完成");
                    scheduleViewHolder.tv_item_check_in.setVisibility(View.VISIBLE);
                    scheduleViewHolder.tv_item_check_in.setText("查看签到");
                    //查看签到
                    scheduleViewHolder.tv_item_check_in.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            //查看签到详情界面
                            context.signInActivity(true, schedule.getScheduleOfficer().getId());
                        }
                    });
                    break;
            }
        }
    }

    /**
     * 当为请假状态时修改颜色
     * @param scheduleViewHolder
     * @param type
     */
    private void changeColor(ScheduleViewHolder scheduleViewHolder, int type) {
        switch (type) {
            case TYPE_POSITIVE_COLOR:
                scheduleViewHolder.tv_item_map.setTextColor(0xFF00ADB5);
                scheduleViewHolder.tv_item_start.setTextColor(0xFF333333);
                scheduleViewHolder.tv_item_end.setTextColor(0xFF333333);
                scheduleViewHolder.tv_item_name.setTextColor(0xFF333333);
                scheduleViewHolder.tv_item_line.setTextColor(0xFF666666);
                scheduleViewHolder.tv_item_fc.setTextColor(0xFF333333);
                scheduleViewHolder.tv_item_bc.setTextColor(0xFF333333);
                scheduleViewHolder.tv_item_bz.setTextColor(0xFF333333);
//                scheduleViewHolder.tv_item_point_start.setTextColor(0xFF666666);
//                scheduleViewHolder.tv_item_point_end.setTextColor(0xFF666666);
//                scheduleViewHolder.tv_item_point_first.setTextColor(0xFF666666);
//                scheduleViewHolder.tv_item_point_last.setTextColor(0xFF666666);

                break;
            case TYPE_GREY_COLOR:
                scheduleViewHolder.view_item.setBackgroundColor(0xFFCCCCCC);
                scheduleViewHolder.tv_item_complete.setTextColor(0xFFCCCCCC);
                scheduleViewHolder.tv_item_map.setTextColor(0xFFCCCCCC);
                scheduleViewHolder.tv_item_map.setOnClickListener(null);
                scheduleViewHolder.tv_item_cycle_driving.setTextColor(0xFFCCCCCC);
                scheduleViewHolder.tv_item_start.setTextColor(0xFFCCCCCC);
                scheduleViewHolder.tv_item_end.setTextColor(0xFFCCCCCC);
                scheduleViewHolder.tv_item_name.setTextColor(0xFFCCCCCC);
                scheduleViewHolder.tv_item_line.setTextColor(0xFFCCCCCC);
                scheduleViewHolder.tv_item_fc.setTextColor(0xFFCCCCCC);
                scheduleViewHolder.tv_item_bc.setTextColor(0xFFCCCCCC);
                scheduleViewHolder.tv_item_bz.setTextColor(0xFFCCCCCC);
//                scheduleViewHolder.tv_item_point_start.setTextColor(0xFFCCCCCC);
//                scheduleViewHolder.tv_item_point_end.setTextColor(0xFFCCCCCC);
//                scheduleViewHolder.tv_item_point_first.setTextColor(0xFFCCCCCC);
//                scheduleViewHolder.tv_item_point_last.setTextColor(0xFFCCCCCC);

                break;
        }
    }

    private void addView(ScheduleViewHolder scheduleViewHolder, List<Shift> list,Schedule schedule){
        scheduleViewHolder.ll_schedule_start.removeAllViews();
        if(list!=null && list.size()>0) {
            scheduleViewHolder.ll_schedule_start.addView(scheduleViewHolder.ll_item_car_times);
            for(int i =0 ;i<list.size();i++) {
                View view = View.inflate(context, R.layout.adapter_item, null);
                TextView tv_item_point_time = view.findViewById(R.id.tv_item_point_time);
                TextView tv_item_point_remark = view.findViewById(R.id.tv_item_point_remark);
                tv_item_point_time.setText(list.get(i).getShiftTime());
                tv_item_point_remark.setText(list.get(i).getRemark());
                if(schedule.getScheduleOfficer().getState() == 2 || schedule.getScheduleOfficer().getState() == 3){
                    tv_item_point_time.setTextColor(0xFFCCCCCC);
                    tv_item_point_remark.setTextColor(0xFFCCCCCC);
                }else{
                    tv_item_point_time.setTextColor(0xFF666666);
                    tv_item_point_remark.setTextColor(0xFF666666);
                }
                scheduleViewHolder.ll_schedule_start.addView(view);
            }
        }
    }

    @Override
    public int getItemCount() {
        return (data == null || data.size() == 0) ? 1 : data.size();
    }

    static class ScheduleViewHolder extends RecyclerView.ViewHolder {
        LinearLayout ll_item_empty;
        //有内容的布局
        LinearLayout ll_item_content;

        TextView tv_item_week;//月份和周
        TextView tv_item_start;//开始时间
        View view_item;//线
        TextView tv_item_cycle_driving;//循环驾驶  定点发车
        TextView tv_item_end;//结束时间
        TextView tv_item_name;//未来一号车
        TextView tv_item_line;//几号线
        TextView tv_item_map;//查看地图

        //定点发车才有的布局 否则隐藏
        LinearLayout ll_schedule_start;
        LinearLayout ll_item_car_times;
        TextView tv_item_fc;//发车
        TextView tv_item_bc;//班次
        TextView tv_item_bz;//备注
//        TextView tv_item_point_start;//开始时间
//        TextView tv_item_point_end;//结束时间
//        TextView tv_item_point_first;//首班
//        TextView tv_item_point_last;//末班

        //已接受
        LinearLayout ll_item_accept;
        TextView tv_item_accept;//接受文字
        TextView tv_item_leave;//请假 可点击
        TextView tv_item_sign_in;//签到 可点击

        //已完成
        LinearLayout ll_item_complete;
        TextView tv_item_complete;//已完成 文字
        TextView tv_item_check_in;//查看签到 可点击


        public ScheduleViewHolder(@NonNull View itemView) {
            super(itemView);
            ll_item_empty = itemView.findViewById(R.id.ll_item_empty);
            ll_item_content = itemView.findViewById(R.id.ll_item_content);

            tv_item_week = itemView.findViewById(R.id.tv_item_week);
            tv_item_start = itemView.findViewById(R.id.tv_item_start);
            view_item = itemView.findViewById(R.id.view_item);
            tv_item_cycle_driving = itemView.findViewById(R.id.tv_item_cycle_driving);
            tv_item_end = itemView.findViewById(R.id.tv_item_end);
            tv_item_name = itemView.findViewById(R.id.tv_item_name);
            tv_item_line = itemView.findViewById(R.id.tv_item_line);
            tv_item_map = itemView.findViewById(R.id.tv_item_map);
            tv_item_fc = itemView.findViewById(R.id.tv_item_fc);
            tv_item_bc = itemView.findViewById(R.id.tv_item_bc);
            tv_item_bz = itemView.findViewById(R.id.tv_item_bz);


            ll_schedule_start = itemView.findViewById(R.id.ll_schedule_start);
            ll_item_car_times = itemView.findViewById(R.id.ll_item_car_times);
//            tv_item_point_start = itemView.findViewById(R.id.tv_item_point_start);
//            tv_item_point_end = itemView.findViewById(R.id.tv_item_point_end);
//            tv_item_point_first = itemView.findViewById(R.id.tv_item_point_first);
//            tv_item_point_last = itemView.findViewById(R.id.tv_item_point_last);

            ll_item_accept = itemView.findViewById(R.id.ll_item_accept);
            tv_item_accept = itemView.findViewById(R.id.tv_item_accept);
            tv_item_leave = itemView.findViewById(R.id.tv_item_leave);
            tv_item_sign_in = itemView.findViewById(R.id.tv_item_sign_in);

            ll_item_complete = itemView.findViewById(R.id.ll_item_complete);
            tv_item_complete = itemView.findViewById(R.id.tv_item_complete);
            tv_item_check_in = itemView.findViewById(R.id.tv_item_check_in);
        }
    }

}
